<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>展开运算符</title>
</head>

<body>
  <script type="text/javascript">

    let arr1 = [1, 3, 5, 7, 9]
    let arr2 = [2, 4, 6, 8, 10]
    // 展开一个数组
    console.log(...arr1)
    // 链接两个数组
    let arr3 = [...arr1, ...arr2]
    console.log(arr3)

    // 函数传参
    function sum(...numbers) {
      return numbers.reduce((preVal, currVal) => {
        return preVal + currVal
      })
    }
    console.log(sum(2, 3, 4, 5))

    // 展开对象报错，展开运算符不能展开一个对象
    let boy = { name: '王肖杰', sex: '男', age: 10 }
    // console.log(...boy)

    // 复制对象  构造字面变量对象时使用展开语法
    let boy1 = { name: '王肖杰', sex: '男', age: 10 }
    let boy2 = { ...boy1 }
    boy1.name = "王俊天"
    console.log(boy1, boy2)

    // 复制对象的同时，修改属性值，其实就是合并
    let boy3 = { name: '王俊宇', sex: '男', age: 4 }
    let boy4 = { ...boy3, name: '王俊天' }
    console.log(boy3, boy4)

  </script>
</body>

</html>